package com.yc.thread.pro1_matrix.row;

import com.yc.thread.pro1_matrix.individual.individualMultiplierTask;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zwei
 * @create 2024-07-17 16:03
 */

/**
 * 并行第二版：结果矩阵每一行一个线程
 */
public class ParalleRowMultiplier {
    public static void multiplt(double[][] matrix1, double[][] matrix2, double[][] result) {
        List<Thread> threads = new ArrayList<>();
        int row1 = matrix1.length;    //第一个矩阵的行数
        int column1 = matrix1[0].length;  //第一个矩阵的列数

        int row2 = matrix2.length;    //第二个矩阵的行数
        int column2 = matrix2[0].length;  //第二个矩阵的列数
        for (int i = 0; i < row1; i++) {
            //创建线程
            Thread t = new Thread(new rowMultiplierTask(result, matrix1, matrix2, i));
            t.start();
            threads.add(t);
            if (threads.size() % 10 == 0) {
                waitForThread(threads);
            }
        }
    }
    private static void waitForThread(List<Thread> threads) {
        for (Thread t:threads){
            try {
                t.join();   //t先运行  其他线程等
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
